<template>
	<el-dialog title="预结账" :visible.sync="preChckVisible" :close-on-click-modal="false" width="45%" center>
		<el-row :gutter="20">
			<el-col :span="8">
				<el-form label-width="80px">
					<el-form-item label="总金额:">
						<div style="color: red;">￥{{ orderInfo.totalMoney }}</div>
					</el-form-item>
					<el-form-item label="应付金额:">
						<span style="color: red;">￥{{ totalMoney }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
						<el-popover @show="toDiscount" placement="right" width="500px" trigger="click" v-model="manuaChangeVisle">
							<ManualDiscountKeyBord @getDiscountMoney="DisCountMoney" ref="ManualDiscountKeyBordREf"></ManualDiscountKeyBord>
							<el-link style="cursor: pointer;color: green" slot="reference" size="mini" plain type="success">手动优惠</el-link>
						</el-popover>
					</el-form-item>
					<el-form-item>
						<div v-show="manualOfferMoney !== 0">手动优惠：-{{manualOfferMoney}}</div>
						<div v-show="memberDiscount !== 0">会员优惠：-{{memberDiscount}}</div>
						<div v-show="discountTotal !== 0">方案优惠：-{{discountTotal}}</div>
					</el-form-item>
					<el-form-item label="抹零:">￥{{ zero }}</el-form-item>
				</el-form>
			</el-col>
			<el-col :span="16">
				<el-form label-width="80px">
					<el-form-item label="会员:"><el-button type="warning" @click="memberList">会员</el-button></el-form-item>
					<el-form-item label="详情:">
						<div style="border: 1px solid wheat;">
							<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
								<el-col :span="12">
									名字：
									<span style="color: green;">{{ memberName }}</span>
								</el-col>
								<el-col :span="12">
									类型：
									<span style="color: green;">{{ memberTypeName }}</span>
								</el-col>
							</el-row>
							<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
								<el-col :span="12">
									余额：
									<span style="color: red;">{{ option.data[0].list[1].value }}</span>
								</el-col>
								<el-col :span="12">
									消费：
									<span style="color: red;">{{ option.data[0].list[2].value }}</span>
								</el-col>
							</el-row>
							<el-row style="padding-top: 1.25rem;padding-left: 1.25rem;">
								<el-col :span="12">积分：{{ option.data[0].list[0].value }}</el-col>
								<el-col :span="12">折扣：{{ discount }}折</el-col>
							</el-row>
							<el-row style="padding-top: 0.625rem;padding-left: 1.25rem;">
								<el-col :span="12"><el-button size="mini" @click="clearMemberInfo" type="infor">清除</el-button></el-col>
							</el-row>
							<br />
						</div>
					</el-form-item>
					<el-form-item label="折扣方案:">
            <el-button type="warning" @click="discountDetailList">折扣方案</el-button>
<!--            <a @click="clearDis">&nbsp;-->
<!--            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;清除方案</a>-->
          </el-form-item>
				</el-form>
			</el-col>
		</el-row>
		<span slot="footer" class="dialog-footer">
		  <el-button @click="preChckVisible = false">取消</el-button>
		  <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>

		<!-- 会员列表弹出层 -->
		<el-dialog center title="会员列表" :visible.sync="memberListVisile" width="70%" append-to-body><MemberList @getSelectMemberData="memberInfo"></MemberList></el-dialog>

		<!-- 折扣方案 -->
		<el-dialog center title="折扣方案列表" :visible.sync="DisCountListVisle" width="40%" append-to-body>
			<DisCountList @DiscountDetail="getDiscountDetailList" ref="DisCountListRef"></DisCountList>
		</el-dialog>
	</el-dialog>
</template>

<script>
import MemberList from '../../modules/store/member/recharge/memberList.vue';
import DisCountList from './DisCountList.vue';
import ManualDiscountKeyBord from './ManualDiscountKeyBord.vue';
import {keepTwoPoint,pointPreNum} from '../../../utils/index'
export default {
	components: {
		MemberList,
		DisCountList,
		ManualDiscountKeyBord
	},

	data() {
		return {
			actualMoney: 0,
			totalMoney: 0,
			zero: 0,
			DisCountListVisle: false,
			preChckVisible: false,
			memberListVisile: false,
			option: {
				span: 24,
				data: [
					{
						title: '',
						subtitle: '',
						img: 'https://avatar.gitee.com/uploads/61/632261_smallweigit.jpg!avatar100?1518660401',
						color: '#00a7d0',
						list: [
							{
								label: '积分',
								value: ''
							},
							{
								label: '余额',
								value: ''
							},
							{
								label: '消费总额',
								value: ''
							},
							{
								label: '折扣',
								value: ''
							}
						]
					}
				]
			},
			discount: '',
			memberName: '',
			memberTypeName: '',
			memberId:null,
			orderInfo: {
				totalMoney:0,
        disOrMemMoney:0
			},
			discountTotal:0,
			discountId:null,
			manualOfferMoney:0,
			memberDiscount:0, //会员优惠金额
			manuaChangeVisle:false,
      bakMoney:0,
      changePriceSign:1,
      changeDiscountSign:1,
      bakChangeDisMoney:0,
      bakChangePriceMoney:0
		};
	},

	methods: {

    clearDis(){

    },

		clearMemberInfo(){
		  if (this.memberId !== null){
        let total = Number(this.totalMoney) + Number(this.memberDiscount) + Number(Math.abs(this.zero))
        if (this.discountId === null){
          this.zero = 0
          this.totalMoney = total
        }else {
          let total = this.totalMoney + this.memberDiscount + Math.abs(this.zero)
          let wipe = this.$wipeZeroRule(total)
          this.totalMoney = wipe
          this.zero = wipe - total
        }
        this.memberDiscount = 0
        this.memberId = null
        this.discount = ''
        this.memberName = ''
        this.memberTypeName = ''
        this.option.data[0].list[0].value =''
        this.option.data[0].list[1].value = ''
        this.option.data[0].list[2].value = ''
      }

		},

		dataFormSubmit(){
			if(this.memberId !== null || this.discountId !== null){
				this.orderInfo.disOrMemMoney = keepTwoPoint(Number(this.memberDiscount) + Number(this.discountTotal))
			}
			if(this.manualOfferMoney > 0){
				this.orderInfo.manualOfferMoney = this.manualOfferMoney
			}
			this.orderInfo.preCheckSign = 1
      if (this.orderInfo.openDeskInfo.master === 1 || this.orderInfo.openDeskInfo.slave === 1){
        this.orderInfo.totalMoney = this.bakMoney
        this.orderInfo.totalMoney -= this.orderInfo.disOrMemMoney
        this.orderInfo.totalMoney -= this.orderInfo.manualOfferMoney
      }
      this.orderInfo.totalMoney = this.$wipeZeroRule(this.totalMoney);
			this.$http({
				url: this.$http.adornUrl('/reception/tstoreopendesk/preCheck'),
				method: 'post',
				data: this.$http.adornData({
					orderInfo: this.orderInfo,
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.$message.success("预结账成功")
					this.preChckVisible = false
					this.$emit('PreCheck')
				} else {
					this.$message({
						message: '系统异常',
						type: 'warning',
						duration: 1500
					});
				}
			});
		},

		//获取优惠金额
		DisCountMoney(money) {
			if(money > this.totalMoney){
				this.$message.info("优惠金额不能大于订单金额")
			}else{
        if (this.changePriceSign === 1){
          this.changePriceSign++
          this.bakChangePriceMoney = this.totalMoney
          this.totalMoney = this.totalMoney - money;
        }else {
          this.totalMoney = this.bakChangePriceMoney - money;
        }
				this.manuaChangeVisle = false
				this.manualOfferMoney = money
			}

		},

		toDiscount() {
			this.$refs.ManualDiscountKeyBordREf.init('优惠金额');
		},


		init(info) {
			this.manualOfferMoney = 0
			this.memberDiscount = 0
			this.discountTotal = 0
			this.discount = '';
			this.memberName = '';
			this.memberTypeName = '';
			this.memberId = null
			this.discountId = null
			this.totalMoney = 0
      this.zero = 0
      //this.orderInfo.totalMoney = 0
      this.bakMoney = info.totalMoney
      this.option.data[0].list[0].value =''
      this.option.data[0].list[1].value = ''
      this.option.data[0].list[2].value = ''
			if (info.openDeskInfo.master === 1 || info.openDeskInfo.slave === 1) {
				this.$http({
					url: this.$http.adornUrl('/reception/tstoreopendesk/queryMergeOrderInfo'),
					method: 'get',
					params: this.$http.adornParams({
						deskId: info.id,
						masterId: info.openDeskInfo.masterId
					})
				}).then(({ data }) => {
					if (data && data.code === 0) {
					  info.totalMoney = 0
            this.orderInfo = info
						var arr = [];
						data.mergeOrderInfo.forEach(item => {
							item.foodVos.forEach(item2 => {
								arr.push(item2);
							});
							this.totalMoney += item.totalMoney;
							this.orderInfo.totalMoney += item.totalMoney
						});
					} else {
						this.$message({
							message: '系统异常',
							type: 'warning',
							duration: 1500
						});
					}
				});
			} else {
        this.orderInfo = info;
				this.orderInfo.totalMoney = info.totalMoney;
				this.totalMoney = info.totalMoney;
			}
			this.preChckVisible = true;
		},

		memberList() {
			this.memberListVisile = true;
		},

		//折扣方案
		discountDetailList() {
			this.DisCountListVisle = true;
			this.$nextTick(() => {
				this.$refs.DisCountListRef.getDataList();
			});
		},

		getDiscountDetailList(row) {
			if(this.discountId === null){
			  this.manualOfferMoney = 0
        this.disOrMemMoney = 0
			  this.orderInfo.preDisSign = 1
				this.discountId = row.discountId
				var discountList = row.list;
				var arr = [];
				this.orderInfo.foodVos.forEach(item => {
					discountList.forEach(item2 => {
						if (item.id === item2.foodId) {
							item.discountMoney = (item.total * item2.discount) / 10
							item.discountSign = 1;
							let mon = item.total - item.discountMoney;
							this.discountTotal += keepTwoPoint(mon)
						}
					});
					arr.push(item);
				});
				this.orderInfo.foodVos = arr
				this.totalMoney -= this.discountTotal
        let wipe = this.$wipeZeroRule(this.totalMoney)
        this.zero += keepTwoPoint(wipe - this.totalMoney)
        this.totalMoney = wipe
        if (Math.abs(this.zero) >= 1){
          let number = pointPreNum(Math.abs(this.zero));
          this.totalMoney = Number(this.totalMoney) + Number(number)
          if (this.zero < 0){
            this.zero =keepTwoPoint( Number(this.zero) + Number(number))
          }else {
            this.zero = keepTwoPoint(Number(this.zero) - Number(number))
          }
        }
				this.DisCountListVisle = false;
			}else{
				this.$message.info("已经使用过方案")
			}

		},

		//打折使用会员
		memberInfo(row) {
			if(this.memberId === null){
				this.memberId = row.memberId
				this.memberListVisile = false;
				this.discount = row.defaultDiscount;
				this.memberName = row.memberName;
				this.orderInfo.preMemSign = 1
				this.memberTypeName = row.memberTypeName;
				this.memberDiscount = keepTwoPoint(this.totalMoney - this.totalMoney*(row.defaultDiscount/10))
				this.totalMoney = keepTwoPoint(this.totalMoney*(row.defaultDiscount/10))
        let wipe = this.$wipeZeroRule(this.totalMoney)
        this.zero += keepTwoPoint(wipe - this.totalMoney)
        this.totalMoney = wipe
        if (Math.abs(this.zero) >= 1){
          let number = pointPreNum(Math.abs(this.zero));
          this.totalMoney = Number(this.totalMoney) + Number(number)
          if (this.zero < 0){
            this.zero =keepTwoPoint( Number(this.zero) + Number(number))
          }else {
            this.zero = keepTwoPoint(Number(this.zero) - Number(number))
          }
        }
				//获取会员账户详情
				this.$http({
					url: this.$http.adornUrl(`/store/tstorememberaccountdetail/info/` + row.id),
					method: 'get',
					params: this.$http.adornParams()
				}).then(({ data }) => {
					if (data && data.code === 0) {
						this.option.data[0].list[0].value = data.tStoreMemberAccountDetail.integral;
						this.option.data[0].list[1].value = data.tStoreMemberAccountDetail.balace;
						this.option.data[0].list[2].value = data.tStoreMemberAccountDetail.totalConsume;
					}
				});
			}else{
				this.$message.info("已经选择过会员")
			}

		}
	}
};
</script>

<style></style>
